package com.pcg.posgasstation.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pcg.posgasstation.R;
import com.pcg.posgasstation.base.BaseActivity;
import com.pcg.posgasstation.model.cache.CacheManager;
import com.pcg.posgasstation.model.network.ApiClient;
import com.pcg.posgasstation.model.network.entity.BaseEntity;
import com.pcg.posgasstation.model.network.entity.StaffInfoEntity;
import com.pcg.posgasstation.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PersonInfoActivity extends BaseActivity {

    @BindView(R.id.layTvBack)
    ImageView mLayTvBack;
    @BindView(R.id.layBack)
    RelativeLayout mLayBack;
    @BindView(R.id.layTvTitle)
    TextView mLayTvTitle;
    @BindView(R.id.tvPersonName)
    TextView mTvPersonName;
    @BindView(R.id.tvPersonPhone)
    TextView mTvPersonPhone;
    @BindView(R.id.tvPersonGroupName)
    TextView mTvPersonGroupName;
    @BindView(R.id.btnExitLogin)
    Button mBtnExitLogin;

    private StaffInfoEntity mPersonInfoEntity;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PersonInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        ButterKnife.bind(this);

        initViews();
    }

    @OnClick(R.id.layBack)
    void backClick() {
        finish();
    }

    @OnClick(R.id.btnExitLogin)
    void exitLoginClick() {
        ApiClient.getApiService().loginout(ApiClient.getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity<String> entity) {
                        if (entity != null && entity.isSuccess()) {
                            exitLogin();
                        } else {
                            if (entity != null) {
                                ToastUtils.showToast(entity.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        exitLogin();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void exitLogin() {
        ApiClient.setToken("");
        CacheManager.getInstance().setLoginedToken("");

        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mContext.startActivity(intent);
        finish();
    }

    private void initViews() {
        mLayTvTitle.setText("我的信息");
        getPersonInfo();
    }

    private void getPersonInfo() {
        ApiClient.getApiService().getStaffInfo(ApiClient.getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity<StaffInfoEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity<StaffInfoEntity> entity) {
                        if (entity != null) {
                            if (entity.isSuccess()) {
                                mPersonInfoEntity = entity.getData();
                                showPersonInfo(mPersonInfoEntity);
                            } else {
                                ToastUtils.showToast(entity.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showPersonInfo(StaffInfoEntity entity) {
        if (entity == null) {
            return;
        }

        mTvPersonName.setText(entity.getName());
        mTvPersonPhone.setText(entity.getPhone());
        mTvPersonGroupName.setText(entity.getGroup_nam());

    }

}
